package demo.base;

import java.util.*;
import java.util.Map.Entry;


public class MapUtils {

	/**
	 * 将键值对调换过来, 值->键, 键->值
	 * @param map
	 * @return
	 */
	public static Map<String, String> keyValueReverse(Map<String, String> map) {
		Map<String, String> ret = new HashMap<String, String>();
		Set<Entry<String, String>> entrySet = map.entrySet();
		for (Entry<String, String> entry : entrySet) {
			ret.put(entry.getValue(),  entry.getKey());
		}
		return ret;
	}

	public static void removeByKeys(Map<String, Object> map,
			List<String> includeKeys) {
		Set<Entry> entrySet = (Set) map.entrySet();
		Iterator<Entry> iterator = entrySet.iterator();
		while (iterator.hasNext()) {
			Entry entry = iterator.next();
			if (!includeKeys.contains(entry.getKey())) {
				iterator.remove();
			}
		}

	}

	public static List<?> getValues(Map map, List keys) {
		List ret = new ArrayList();
		for (Object key : keys) {
			ret.add(map.get(key));
		}
		return ret;
	}

	public static Map from(Object... objects) {
		Map<Object, Object> map = new ArrayListMap<Object, Object>();
		for (int i = 0; i < objects.length; i+=2) {
			map.put(objects[i], objects[i+1]);
		}
		return map;
	}

}
